<?php
namespace app\admin\controller;

use app\core\Incontroller;
use think\Db;
use think\Request;
use Qiniu\Auth;

class Shop extends Incontroller
{

    public function _empty($name){
        return $name;
    }

    public function index(){
        if(isset($_POST['draw']) && $_POST['draw']){
            $page_start = intval($_POST['start']);
            $length = intval($_POST['length']);
            $shop = model('shop');

            if(isset($_POST['search']['value'])&&$_POST['search']['value']){
                $count = $shop->whereLike('title','%'.$_POST['search']['value'].'%')->count();
                $res   = $shop->whereLike('title','%'.$_POST['search']['value'].'%')->limit($page_start,$length)->order('id','desc')->select();
            }else{
                $count = $shop->count("*");
                $res = $shop->limit($page_start,$length)->order('id','desc')->select();
            }

            $data = [];
            foreach ($res as $val){
                $data[] = array(
                    'id'          => $val->id,
                    'thumb'       => $val->thumb,
                    'title'       => $val->title,
                    'price'       => $val->price,
                    'pay_price'   => $val->pay_price,
                    'like_num'    => $val->like_num,
                    'fav_num'     => $val->fav_num,
                    'click_num'   => $val->click_num,
                    'create_time' => $val->create_time,
                    'sid'         => $val->id
                );
            }
            echo json_encode([
                'draw'            => $_POST['draw'],
                'recordsTotal'    => $count,
                'recordsFiltered' => $count,
                'data'            => $data
            ]);
            exit;
        }

        return $this->fetch('shop/index');
    }

    public function add(){
        $qiniu = config('qiniu');
        //添加表单提交
        if(isset($_POST['type']) && $_POST['type']=='save'){
            $data = $this->_getData($qiniu);

            $shop = model('shop');
            $res = $shop->insert($data);
            if($res){
                error(0,'添加成功');
                exit;
            }
            error(1,'添加失败');
            exit;
        }


        vendor('qiniu/php-sdk/autoload.php');
        $auth = new Auth($qiniu['accessKey'],$qiniu['secretKey']);
        $token = $auth->uploadToken($qiniu['bucket']); //七牛云上传凭证

        //查询系统分类
        $type = model('type');
        $type =$type->getSysAll();

        //查询系统标签
        $flag = model('flag');
        $flag =$flag->getSysAll();

        $this->assign('qiniu_token',$token);
        $this->assign('type',$type);
        $this->assign('flag',$flag);
        return $this->fetch('shop/add');
    }

    public function edit(){
        $qiniu = config('qiniu');
        $shop = model('shop');
        //添加表单提交
        if(isset($_POST['type']) && $_POST['type']=='save'){
            $data = $this->_getData($qiniu);
            unset($data['create_time']);
            $res = $shop->update($data,['id'=>intval($_POST['id'])]);
            if($res){
                error(0,'修改成功');
                exit;
            }
            error(1,'修改失败');
            exit;
        }


        vendor('qiniu/php-sdk/autoload.php');
        $auth = new Auth($qiniu['accessKey'],$qiniu['secretKey']);
        $token = $auth->uploadToken($qiniu['bucket']); //七牛云上传凭证

        //查询系统分类
        $type = model('type');
        $type =$type->getSysAll();

        //查询系统标签
        $flag = model('flag');
        $flag =$flag->getSysAll();

        $shop = $shop->where('id',intval($_GET['id']))->find();

        $this->assign('qiniu_token',$token);
        $this->assign('type',$type);
        $this->assign('flag',$flag);
        $this->assign('shop',$shop);
        echo $this->fetch('shop/edit');
        exit;
    }

    public function del(){  //删除
        if(!isset($_POST['id']) || !$_POST['id']){
            error(1,'删除失败');
            exit;
        }
        $shop = model('shop');
        $del = $shop->where(['id'=>intval($_POST['id'])])->delete();
        if($del){
            error(0,'删除成功');
            exit;
        }
        error(1,'删除失败');

    }

    public function typelist(){  //分类列表
        if(isset($_POST['draw']) && $_POST['draw']){
            $page_start = intval($_POST['start']);
            $length = intval($_POST['length']);
            $shop = model('type');

            if(isset($_POST['search']['value'])&&$_POST['search']['value']){
                $count = $shop->whereLike('name','%'.$_POST['search']['value'].'%')->count();
                $res   = $shop->whereLike('name','%'.$_POST['search']['value'].'%')->limit($page_start,$length)->order('id','desc')->select();
            }else{
                $count = $shop->count("*");
                $res = $shop->limit($page_start,$length)->order('id','desc')->select();
            }

            $data = [];
            foreach ($res as $val){
                $data[] = array(
                    'id'         => $val->id,
                    'name'       => $val->name,
                    'en_name'    => $val->en_name,
                    'create_time'=> $val->create_time,
                    'sid'        => $val->id,
                );
            }
            echo json_encode([
                'draw'            => $_POST['draw'],
                'recordsTotal'    => $count,
                'recordsFiltered' => $count,
                'data'            => $data
            ]);
            exit;
        }

        return $this->fetch('shop/typelist');
    }

    public function addType(){
        $qiniu = config('qiniu');
        //添加表单提交
        if(isset($_POST['type']) && $_POST['type']=='save'){
            $data = $this->_getTypeData($qiniu);

            $shop = model('type');
            $res = $shop->insert($data);
            if($res){
                error(0,'添加成功');
                exit;
            }
            error(1,'添加失败');
            exit;
        }


        vendor('qiniu/php-sdk/autoload.php');
        $auth = new Auth($qiniu['accessKey'],$qiniu['secretKey']);
        $token = $auth->uploadToken($qiniu['bucket']); //七牛云上传凭证

        $type = model('type');
        $type = $type->field('name,en_name')->where('p_en_name is null or p_en_name=""')->select();

        $this->assign('type',$type);
        $this->assign('qiniu_token',$token);
        return $this->fetch('shop/addtype');
    }

    public function editType(){
        $qiniu = config('qiniu');
        $shop = model('type');
        //添加表单提交
        if(isset($_POST['type']) && $_POST['type']=='save'){
            $data = $this->_getTypeData($qiniu);
            unset($data['create_time']);
            $res = $shop->update($data,['id'=>intval($_POST['id'])]);
            if($res){
                error(0,'修改成功');
                exit;
            }
            error(1,'修改失败');
            exit;
        }


        vendor('qiniu/php-sdk/autoload.php');
        $auth = new Auth($qiniu['accessKey'],$qiniu['secretKey']);
        $token = $auth->uploadToken($qiniu['bucket']); //七牛云上传凭证

        $shop = $shop->where('id',intval($_GET['id']))->find();

        $type = model('type');
        $type = $type->field('name,en_name')->where('p_en_name is null or p_en_name=""')->select();

        $this->assign('type',$type);

        $this->assign('qiniu_token',$token);
        $this->assign('shop',$shop);
        echo $this->fetch('shop/edittype');
        exit;
    }

    public function delType(){  //删除
        if(!isset($_POST['id']) || !$_POST['id']){
            error(1,'删除失败');
            exit;
        }
        $shop = model('type');
        $del = $shop->where(['id'=>intval($_POST['id'])])->delete();
        if($del){
            error(0,'删除成功');
            exit;
        }
        error(1,'删除失败');

    }

    protected function _getTypeData($qiniu){
        $data = array(
            'name'       => $_POST['name'],
            'en_name'    => $_POST['en_name'],
            'p_en_name'  => $_POST['p_en_name'],
            'thumb'      => $_POST['thumb'] ? $this->_getImgAllPath($_POST['thumb'],$qiniu):'',
            'show_thumb' => $_POST['show_thumb'] ? $this->_getImgAllPath($_POST['show_thumb'],$qiniu):'',
            'create_time'=> date("Y-m-d H:i:s"),
            'update_time'=> date("Y-m-d H:i:s"),
            'user_id'    => isset($_POST['user_id']) ? $_POST['user_id']:0,
            'description'=> htmlspecialchars($_POST['description'])
        );
        return $data;
    }

    protected function _getImgAllPath($path,$qiniu){
        return strstr($path,'http://')? $path:$qiniu['domain'].$path;
    }

    public function flaglist(){  //分类列表
        if(isset($_POST['draw']) && $_POST['draw']){
            $page_start = intval($_POST['start']);
            $length = intval($_POST['length']);
            $shop = model('flag');

            if(isset($_POST['search']['value'])&&$_POST['search']['value']){
                $count = $shop->whereLike('name','%'.$_POST['search']['value'].'%')->count();
                $res   = $shop->whereLike('name','%'.$_POST['search']['value'].'%')->limit($page_start,$length)->order('id','desc')->select();
            }else{
                $count = $shop->count("*");
                $res = $shop->limit($page_start,$length)->order('id','desc')->select();
            }

            $data = [];
            foreach ($res as $val){
                $data[] = array(
                    'id'         => $val->id,
                    'name'       => $val->name,
                    'en_name'    => $val->en_name,
                    'create_time'=> $val->create_time,
                    'sid'        => $val->id,
                );
            }
            echo json_encode([
                'draw'            => $_POST['draw'],
                'recordsTotal'    => $count,
                'recordsFiltered' => $count,
                'data'            => $data
            ]);
            exit;
        }

        return $this->fetch('shop/flaglist');
    }

    public function addFlag(){
        //添加表单提交
        if(isset($_POST['type']) && $_POST['type']=='save'){
            $data = $this->_getFlagData();

            $shop = model('flag');
            $res = $shop->insert($data);
            if($res){
                error(0,'添加成功');
                exit;
            }
            error(1,'添加失败');
            exit;
        }

        return $this->fetch('shop/addflag');
    }

    public function editFlag(){
        $shop = model('flag');
        //添加表单提交
        if(isset($_POST['type']) && $_POST['type']=='save'){
            $data = $this->_getFlagData();
            unset($data['create_time']);
            $res = $shop->update($data,['id'=>intval($_POST['id'])]);
            if($res){
                error(0,'修改成功');
                exit;
            }
            error(1,'修改失败');
            exit;
        }

        $shop = $shop->where('id',intval($_GET['id']))->find();
        $this->assign('shop',$shop);
        echo $this->fetch('shop/editflag');
        exit;
    }

    public function delFlag(){  //删除
        if(!isset($_POST['id']) || !$_POST['id']){
            error(1,'删除失败');
            exit;
        }
        $shop = model('flag');
        $del = $shop->where(['id'=>intval($_POST['id'])])->delete();
        if($del){
            error(0,'删除成功');
            exit;
        }
        error(1,'删除失败');

    }

    protected function _getFlagData(){
        $data = array(
            'name'      => $_POST['name'],
            'en_name'   => $_POST['en_name'],
            'create_time'=> date("Y-m-d H:i:s"),
            'update_time'=> date("Y-m-d H:i:s"),
            'user_id'    => isset($_POST['user_id']) ? $_POST['user_id']:0
        );
        return $data;
    }

    protected function _getData($qiniu){
        $data = array(
            'title'      => $_POST['title'],
            'price'      => $_POST['price'],
            'pay_price'  => $_POST['pay_price'],
            'type_id'    => ','.$_POST['type_id'].',',
            'flag_id'    => ','.$_POST['flag_id'].',',
            'description'=> $_POST['description'],
            'coupon_url' => $_POST['coupon_url'],
            'thumb'      => strstr($_POST['thumb'],'http://')? $_POST['thumb']:$qiniu['domain'].$_POST['thumb'],
            'go_url'     => $_POST['go_url'],
            'content'    => $_POST['content'],
            'create_time'=> date("Y-m-d H:i:s"),
            'update_time'=> date("Y-m-d H:i:s"),
            'user_id'    => isset($_POST['user_id']) ? $_POST['user_id']:0
        );
        return $data;
    }

}